

  ////////////////////////////////////////////////////////
///////////////// OpenGL Camera ///////////////////////
//////////////////////////////////////////////////////
import processing.opengl.*;
import javax.media.opengl.GL;

PVector cameraPos;
PVector cameraLeft, cameraRight;

static int halfEyeDist = 10;

PShape rocket;
PGL pgl;

void setup(){
  size(800, 600, P3D);

    cameraPos = new PVector(0, 0, 0);
    cameraLeft = new PVector(-halfEyeDist, 0, 0);
    cameraRight = new PVector(halfEyeDist, 0, 0);

    rocket = loadShape("rocket.obj");
}

void draw(){
  background(100);

    pgl = beginPGL();

    /////////////// Analglyph ////////////////////
    // Draw only the red channel
    pgl.colorMask(true,false,false,true); 
  
    setCamera(cameraLeft);
    drawScene();

    // Clear the depth buffer, and set the other mask
    pgl.clear( GL.GL_DEPTH_BUFFER_BIT); 

    // Draw only the blue channel
    pgl.colorMask(false, true, true, true); 

    setCamera(cameraRight);
    drawScene();

    // Put the color mask back to normal. 
    pgl.colorMask(true, true, true, true);
    /////////////// Analglyph -- END ////////////////////

    endPGL();
}



void setCamera(PVector cameraPos){

    PGraphicsOpenGL graphics = (PGraphicsOpenGL) g;
    
    graphics.camera(cameraPos.x, cameraPos.y, cameraPos.z,
        cameraPos.x, cameraPos.y, 0,
        0, 1, 0);

    float nearFactor = nearPlane / cameraPos.z;
    float left   = nearFactor * (- screenSize.x / 2f - cameraPos.x);
    float right  = nearFactor * (  screenSize.x / 2f - cameraPos.x);
    float top    = nearFactor * (  screenSize.y / 2f - cameraPos.y);
    float bottom = nearFactor * (- screenSize.y / 2f - cameraPos.y);

    //    println("Proj befor");
    //    graphics.printProjection();

    graphics.frustum(left, right, bottom, top, nearPlane, farPlane);

    graphics.projection.m11 = -graphics.projection.m11;
    //    println("Proj after");
    //    graphics.printProjection();
}


void drawScene(){

    pushMatrix();

      translate(0, 0, -500);

      rotateX(millis() / 1000f);
      rotateY(millis() / 1200f);
      rotateZ(millis() / 9000f);

      // rocket size 40.
      scale(40);

      scale(1f / 400f);
      //  rotateZ(PI);
      shape(rocket);


    popMatrix();

}



